<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" :title="getTitle" width="800" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm" >
      <template #selectAddress="{ model, field }">
        <a-input v-model:value="model[field]" readonly placeholder="请设置门店地址" @click="handleOpenSelectMapAddress(model[field])"/>
      </template>
    </BasicForm>
  </BasicDrawer>

  <SelectMapAddress @register="registerSelectMapAddressModal" @success="handleSelectAddressSuccess"/>
  
</template>
<script lang="ts" setup>
  import { ref, computed, unref,useAttrs } from 'vue';
  import { useModal } from '/@/components/Modal';
  import {BasicForm, useForm} from '/@/components/Form/index';
  import {formSchema} from './store.data';
  import {BasicDrawer, useDrawerInner} from '/@/components/Drawer';
  import {saveOrUpdateStore} from './store.api';
  import SelectMapAddress from '/@/components/Form/src/my/components/SelectMapAddress/index.vue';
  // 声明Emits
  const emit = defineEmits(['success', 'register']);
  const attrs = useAttrs()
  const isUpdate = ref(true);
  const rowId = ref('');
  const selectAddress = ref<any>({})

  const [registerSelectMapAddressModal, {openModal: openSelectMapAddressModal}] = useModal();

  //表单配置
  const [registerForm, {setProps, resetFields, setFieldsValue, validate, updateSchema}] = useForm({
    labelWidth: 90,
    schemas: formSchema,
    labelCol: {
      md: { span: 4 },
      sm: { span: 6 },
    },
    wrapperCol: {
      md: { span: 20 },
      sm: { span: 18 },
    },
    showActionButtonGroup: false,
  });
  

  //表单赋值
  const [registerDrawer, {setDrawerProps, closeDrawer}] = useDrawerInner(async (data) => {
    await resetFields();
    setDrawerProps({ confirmLoading: false, showFooter: true })
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      rowId.value = data.record.id;
    } else {
      // 设置默认值
      data.record = {}
      data.record.openTime = [
        {
          openWeek: ['1', '2', "3", '4', '5', '6', '7'],
          startTime: '06:00',
          endTime: '22:00',
        }
      ]
    }
    selectAddress.value.addressDetail = data.record.addressDetail

     if (typeof data.record === 'object') {
      setFieldsValue({
        ...data.record, openTime: JSON.stringify(data.record.openTime), tags: data.record.tags?.split(',')
      })
    }
  });
  //获取标题
  const getTitle = computed(() => isUpdate.value ? '编辑门店' : '创建门店');

  const handleOpenSelectMapAddress = (address) => {
    selectAddress.value.address = address
    openSelectMapAddressModal(true, { selectAddress: selectAddress.value })
  }
  //提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      console.log('values', values);

      setDrawerProps({confirmLoading: true});
      await saveOrUpdateStore({
        ...values, 
        openTime: JSON.parse(values.openTime), 
        addressDetail: unref(selectAddress).addressDetail
      }, isUpdate.value);
      closeDrawer();
      emit('success', {values: {...values, id: rowId.value}});
    } finally {
      setDrawerProps({confirmLoading: false});
    }
  }

  const handleSelectAddressSuccess = (useAddress) => {
    console.log(`selectAddress is`, useAddress);
    selectAddress.value = useAddress
    setFieldsValue({ ...useAddress });
  }
</script>
